package com.lovo.sgproj.frame.roommanage;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTxt;
import com.lovo.sgproj.Config;
import com.lovo.sgproj.bean.RecordBean;
import com.lovo.sgproj.bean.RoomBean;
import com.lovo.sgproj.frame.MainFrame;
import com.lovo.sgproj.service.RecordService;
import com.lovo.sgproj.service.RoomService;
import com.lovo.sgproj.service.imp.RoomServiceImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class RoomManagePanel extends JPanel {

    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

    private MainFrame frame;

    private LovoTable roomTab;

    private LovoButton addRoomBtn;
    private LovoButton delRoomBtn;
    private LovoButton roomInfoBtn;
    private LovoButton roomEqpBtn;

    private JPanel queryPanel;
    private JLabel roomAddressLab;
    private JTextField roomAddressTxt;
    private LovoComboBox<String> roomStatCmb;
    private LovoComboBox<String> liveableCmb;
    private LovoComboBox<String> roomTypeCmb;
    private JButton queryBtn;

    private int id;

    public RoomManagePanel(MainFrame frame) {
        this.frame = frame;
        this.setBackground(Color.WHITE);
        this.setLayout(null);
        this.initTable();
        this.initBtn();
        this.initQueryPanel();
    }

    private void initTable() {
        // TODO Auto-generated method stub
        this.roomTab = new LovoTable(this, new String[]{"房间地址", "可容纳人数",
                "已住人数", "房租", "房间状态", "房东", "房东电话", "房间类型"}, new String[]{"address", "maxpeople", "peoplenow", "rent", "roomstatus",
                "owner", "ownerphone", "roomtype"},// 数组中应该是对应属性的属性名
                "roomID");// 填入唯一标示属性
        this.roomTab.setSizeAndLocation(0, 0, 800, 300);
    }

    public void addRoomToTab(ArrayList<RoomBean> roomLst) {
        this.roomTab.updateLovoTable(roomLst);
        for (int i = 0; i < roomLst.size(); i++) {
            this.roomTab.setValueAt(roomLst.get(i).getRoomstatus() ? "正常" : "设施损坏", i, 4);
            this.roomTab.setValueAt(roomLst.get(i).getRoomtype() ? "男生寝室" : "女生寝室", i, 7);
        }
    }

    private void initBtn() {
        // TODO Auto-generated method stub
        this.addRoomBtn = new LovoButton("添加房间", 15, 380, this);
        this.delRoomBtn = new LovoButton("删除房间", 160, 380, this);
        this.roomInfoBtn = new LovoButton("查看房间信息", 15, 450, this);
        this.roomEqpBtn = new LovoButton("查看房间设施", 160, 450, this);

        this.addRoomBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                new RoomAddDialog(RoomManagePanel.this.frame);
            }
        });

        this.delRoomBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                id = RoomManagePanel.this.roomTab.getKey();
                RoomService roomService = context.getBean(RoomServiceImp.class);
                if (roomService.removeRoom(id)) {
                    frame.getRoomNode().addListener();
                } else {
                    new RoomDelDialog(RoomManagePanel.this.frame);
                }
            }
        });

        this.roomInfoBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                id = RoomManagePanel.this.roomTab.getKey();
                new RoomInfoDialog(RoomManagePanel.this.frame, id);
            }
        });

        this.roomEqpBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                id = RoomManagePanel.this.roomTab.getKey();
                //System.out.println(id);
                new RoomEqpDialog(RoomManagePanel.this.frame, id);
            }
        });
    }

    private void initQueryPanel() {
        // TODO Auto-generated method stub
        this.queryPanel = new JPanel();
        this.queryPanel.setBorder(BorderFactory.createTitledBorder("查询房间信息"));
        this.queryPanel.setBackground(Color.WHITE);
        this.queryPanel.setLayout(null);
        this.queryPanel.setBounds(300, 310, 480, 180);
        this.add(this.queryPanel);

        this.roomAddressLab = new JLabel("房间地址");
        this.roomAddressLab.setBounds(20, 20, 60, 20);
        this.queryPanel.add(this.roomAddressLab);
        this.roomAddressTxt = new JTextField();
        this.roomAddressTxt.setBounds(100, 20, 120, 20);
        this.queryPanel.add(this.roomAddressTxt);

        ArrayList<String> statLst = new ArrayList<String>();
        statLst.add("不限");
        statLst.add("正常");
        statLst.add("损坏");
        this.roomStatCmb = new LovoComboBox<String>("房间状态", statLst, 260, 20,
                this.queryPanel);

        ArrayList<String> liveLst = new ArrayList<String>();
        liveLst.add("不限");
        liveLst.add("可住房间");
        this.liveableCmb = new LovoComboBox<String>("可住房间", liveLst, 20, 80,
                this.queryPanel);

        ArrayList<String> typeLst = new ArrayList<String>();
        typeLst.add("不限");
        typeLst.add("男生寝室");
        typeLst.add("女生寝室");
        this.roomTypeCmb = new LovoComboBox<String>("房间类型", typeLst, 260, 80,
                this.queryPanel);

        this.queryBtn = new JButton("查找");
        this.queryBtn.setBounds(220, 130, 80, 20);
        this.queryPanel.add(this.queryBtn);

        this.queryBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RoomService roomService = new RoomServiceImp();
                String address = roomAddressTxt.getText();
                int status = 0;
                if (roomStatCmb.getItem().equals("正常")) {
                    status = 1;
                } else if (roomStatCmb.getItem().equals("不限")) {
                    status = -1;
                }
                int available = 0;
                if (liveableCmb.getItem().equals("不限")) {
                    available = -1;
                }
                int type = 0;
                if (roomTypeCmb.getItem().equals("男生寝室")) {
                    type = 1;
                } else if (roomTypeCmb.getItem().equals("不限")) {
                    type = -1;
                }

                ArrayList<RoomBean> roomLst = roomService.showRoomByCondition(address, status, available, type);
                frame.getRmP().addRoomToTab(roomLst);
            }
        });
    }
}
